package com.zoe.onelink.ratelimit.rule.register.impl;

import com.google.common.util.concurrent.RateLimiter;
import com.zoe.onelink.ratelimit.rule.register.RateLimitRuleRegister;
import com.zoe.onelink.ratelimit.sepc.impl.GuavaApiRateLimiter;
import io.opensergo.proto.fault_tolerance.v1.RateLimitStrategy;
import lombok.RequiredArgsConstructor;

import java.time.Duration;
import java.util.List;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-12-07
 */
@RequiredArgsConstructor
public class GuavaRateLimitRuleRegister implements RateLimitRuleRegister {

    private final GuavaApiRateLimiter guavaApiRateLimiter;

    @SuppressWarnings("UnstableApiUsage")
    @Override
    public void registerRules(List<RateLimitStrategy> rateLimitStrategies) {
        for (RateLimitStrategy rateLimitStrategy : rateLimitStrategies) {
            // 匀速限流
            RateLimiter rateLimiter = RateLimiter.create(rateLimitStrategy.getThreshold());
            this.guavaApiRateLimiter.getRateLimiterMap().put(rateLimitStrategy.getName(), rateLimiter);
        }
    }
}
